package method;

import domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        //1.搭建主界面菜单
        Scanner sc = new Scanner(System.in);
        //定义一个集合容器用来管理系统中的所有信息
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            String choice = sc.next();

            switch (choice) {//搭建选择分支
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStuden(list);
                    break;
                case "3":
                    updateStuden(list);
                    break;
                case "4":
                    // System.out.println("4 查看学生");
                    queryStudents(list);
                    break;
                case "5":
                    System.out.println("5 感谢您的使用");
                    return;//在用户输入退出后结束main方法达到退出效果
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

    public static void addStudent(ArrayList<Student> list) {//添加方法
        Scanner sc = new Scanner(System.in);

        String sid;
     /*  ArrayList<String >sidList = new ArrayList<>();//建立学号的集合
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            sidList.add(s.getSid());
        }
        for (int i = 0; i < sidList.size(); i++) {
            if(sidList.get(i).equals(sid)){
                System.out.println("学号已存在,请重试");
                return;
            }
        }*/
        /*if (same(list, sid)) {//调用same判断方法
            System.out.println("学号已存在,请重试!");
            return;
        }*/
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            int index = getIndex(list, sid);
            if (index == -1) {
                break;
            } else {
                System.out.println("您输入的学号已被占用,请重试!");

            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Student stu = new Student(sid, name, age, birthday);
        list.add(stu);
        System.out.println("添加成功");
    }

    public static void deleteStuden(ArrayList<Student> list) {//删除方法
        if (list.size() == 0) {
            System.out.println("无信息,请添加后重新查询");
            return;
        }
        System.out.println("请输入您要删除的学生学号");
        Scanner sc = new Scanner(System.in);
        String deleteSid = sc.next();
        int index = getIndex(list, deleteSid);
        if (index == -1) {
            System.out.println("您输入的学号不存在,请重新输入");
            return;
        } else {
            list.remove(index);
            System.out.println("删除成功!");
        }
    }

    public static void updateStuden(ArrayList<Student> list) {//修改方法
        if (list.size() == 0) {
            System.out.println("无信息,请添加后重新查询");
            return;
        }
        System.out.println("请输入您要修改的学生学号");
        Scanner sc = new Scanner(System.in);
        String updateSid = sc.next();
        int index = getIndex(list, updateSid);//查找索引
        if (index == -1) {
            System.out.println("您输入的学号不存在,请重新输入");//不存在则返回
            return;
        } else {

            System.out.println("请输入姓名");
            String name = sc.next();
            System.out.println("请输入年龄");
            int age = sc.nextInt();
            System.out.println("请输入生日");
            String birthday = sc.next();
            Student stu = new Student(updateSid, name, age, birthday);

            list.set(index, stu);
            System.out.println("修改成功!");
        }
    }


    public static void queryStudents(ArrayList<Student> list) {//查询方法
        if (list.size() == 0) {
            System.out.println("无信息,请添加后重新查询");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日\t");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
        }
    }

    public static int getIndex(ArrayList<Student> list, String sid) {//学号查索引方法
        int index = -1;

        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String id = stu.getSid();
            if (id.equals(sid)) {
                index = i;
            }
        }
        return index;
    }

    public static boolean same(ArrayList<Student> list, String sid) {//查重方法
        boolean c = false;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String id = stu.getSid();
            if (id.equals(sid)) {
                c = true;
            }
        }
        return c;
    }
}